package com.xunxi.mimi.ui.base;

import android.view.View;
import android.view.ViewGroup;


import com.xunxi.mimi.widget.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseSingleAdapter<T> extends BaseAdapter<T> {

    public BaseSingleAdapter(int layoutResId, List<T> data) {
        super(layoutResId, data);
        setHasStableIds(true);
    }

    public BaseSingleAdapter(int layoutResId) {
        super(layoutResId, null);
        setHasStableIds(true);
    }

    public BaseSingleAdapter(List<T> data) {
        super(0, data);
        setHasStableIds(true);
    }

    public BaseSingleAdapter() {
        super(0, null);
        setHasStableIds(true);
    }

    @Override
    protected abstract void convert(BaseViewHolder holder, T item);

    @Override
    protected BaseViewHolder createBaseViewHolder(View view) {
        return new BaseViewHolder(view);
    }

    @Override
    protected BaseViewHolder onCreateDefViewHolder(ViewGroup parent, int viewType) {
        return createBaseViewHolder(parent, getLayoutRes(viewType));
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int positions) {
        try {
            super.onBindViewHolder(holder, positions);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected int getLayoutRes(int viewType) {
        return mLayoutResId;
    }

    public void removeData(T item) {
        int indexOf = getData().indexOf(item);
        if(indexOf != -1) {
            remove(indexOf);
        }
    }

    public boolean containsData(T item) {
        return getData().contains(item);
    }

    public int getItemPosition(T item) {
        return getData().indexOf(item);
    }

    public void removeDatas(int position, int count) {
        try {
            mData.removeAll(new ArrayList<>(mData.subList(position, count)));
            notifyItemRangeRemoved(position, count);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
